package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzx.api.CommonResult;
import com.lzx.dao.CartDao;
import com.lzx.dto.ShopingCart;
import com.lzx.entity.Cart;
import com.lzx.entity.Pictrue;
import com.lzx.entity.Product;
import com.lzx.service.CartService;
import com.lzx.service.PictrueService;
import com.lzx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Cart)表控制层
 *
 * @author makejava
 * @since 2021-02-23 17:31:01
 */
@RestController
@RequestMapping("/shoppingCart")
public class CartController {
    /**
     * 服务对象
     */
    @Resource
    private CartService cartService;
    @Autowired
    private ProductService productService;
    @Autowired
    private PictrueService pictrueService;

    /**
     * 通过用户id查询用户购物车数据
     *
     * @param memberId 用户ID
     * @return 购物车数据
     */
    @RequestMapping("/getShoppingCart")
    public CommonResult<List<ShopingCart>> getShoppingCart(Integer memberId, Integer current, Integer size) {
        Page<Cart> page = new Page<>(1, 1000);
        Page<Cart> cartPage = cartService.listPage(page, new QueryWrapper<Cart>().eq("memberid", memberId));
        List<Cart> cartList = cartPage.getRecords();
        ArrayList<ShopingCart> shopingCarts = new ArrayList<>();
        if (cartList != null) {
            for (Cart cart : cartList) {
                ShopingCart shopingCart = new ShopingCart();
                shopingCart.setCartId(cart.getId());
                shopingCart.setCount(cart.getCount());
                shopingCart.setMemberId(cart.getMemberid());
                shopingCart.setTotal(cart.getTotal());
                shopingCart.setIsCheck(cart.getIscheck());
                Integer productid = cart.getProductid();
                Product product = productService.getById(productid);
                Pictrue img = pictrueService.getDefaultImg("product", productid);

                product.setImg(img.getSrc());
                shopingCart.setProduct(product);
                shopingCarts.add(shopingCart);
            }
            return CommonResult.success(shopingCarts);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 结算后移除购物车中数据
     *
     * @param carts
     * @return
     */
    @RequestMapping("/deleteShoppingCart")
    public CommonResult deleteCart(@RequestBody List<ShopingCart> carts) {
        boolean result = cartService.removeByIds(carts.stream().map(ShopingCart::getCartId).collect(Collectors.toList()));
        if (!result) {
            return CommonResult.failed();
        }
        return CommonResult.success("删除成功！");
    }

    @RequestMapping("/deleteShoppingCartById")
    public CommonResult deleteCart(Integer cartId) {
        boolean result = cartService.removeById(cartId);
        if (!result) {
            return CommonResult.failed();
        }
        return CommonResult.success("删除成功！");
    }

    /**
     * 更新购物车信息
     *
     * @param newShoppingCart
     * @return
     */
    @RequestMapping("/updateShoppingCart")
    public CommonResult updateShoppingCart(@RequestBody List<ShopingCart> newShoppingCart) {
        newShoppingCart.forEach(cart -> {
            Cart cart1 = cartService.getById(cart.getCartId());
            cart1.setCount(cart.getCount());
            cart1.setIscheck(cart.getIsCheck());
            cart1.setTotal(cart.getProduct().getPrice().multiply(new BigDecimal(cart.getCount())));
            cartService.update(cart1);
        });
        return CommonResult.success(200, "更新成功");
    }

    @PostMapping("/addShoppingCart")
    public CommonResult addShoppingCart(Integer userId, Integer productId) {
        Cart haveProduct = cartService.haveProduct(userId, productId);
        if (BeanUtil.isNotEmpty(haveProduct)) {
            haveProduct.setCount(haveProduct.getCount() + 1);
            cartService.update(haveProduct);
            return CommonResult.success(201, "添加成功");
        } else {
            Cart cart = new Cart();
            cart.setCount(1);
            cart.setProductid(productId);
            cart.setTotal(productService.getById(productId).getPrice());
            cart.setMemberid(userId);
            cart.setIscheck(false);
            cartService.save(cart);
            ShopingCart shopingCart = new ShopingCart();
            shopingCart.setIsCheck(cart.getIscheck());
            shopingCart.setMemberId(cart.getMemberid());
            shopingCart.setCount(cart.getCount());
            shopingCart.setTotal(cart.getTotal());
            shopingCart.setCartId(cart.getId());
            shopingCart.setProduct(productService.getById(cart.getProductid()));
            return CommonResult.success(shopingCart);
        }


    }

}
